package sg.nus.iss.vmcs.controllers;

import sg.nus.iss.vmcs.models.Coin;
import sg.nus.iss.vmcs.models.CoinRegistry;


/**
 * List of Coin Handlers used to check coin inputs. 
 * @author Raj
 */
public class CoinHandlerList {

	/**
	 * Static Array to hold the list of handlers
	 */
	private static CoinHandler[] handlers = new CoinHandler[0] ;
	
	/**
	 * Initializes a chain of handlers used for checking coins
	 * @return First Handler for the chain of handlers
	 */
	public static CoinHandler getFirstHandler(){
		Coin[] stdCoins = CoinRegistry.getInstance().getAllCoins();
		 handlers = new CoinHandler[stdCoins.length];
		
		for(int i = 0; i < stdCoins.length; i++) {
			CoinHandler handler = new ConcreteCoinHandler(stdCoins[i]);
			if(i > 0) { 
				handlers[i - 1].setSuccessor(handler);
				System.out.println(stdCoins[i].getName() + "Handler is set as successor of " + stdCoins[i-1].getName() +"Handler");
			}
			else{
				System.out.println("The first Handler is " +stdCoins[i].getName() +"Handler" );
			}
			handlers[i] = handler;
		}
		
		if(handlers.length > 0) {
			
			return handlers[0];
		}
		else {
			return null;
		}
	}
}
